package com.wms.controller;

import com.wms.domain.Dep;
import com.wms.domain.Result;
import com.wms.service.DepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
public class DepController {
    @Autowired
    private DepService depService;

    @GetMapping("/depts")
    public Result findAll(){
        log.info("查询全部部门数据");
        List<Dep> depList = depService.find();
        return Result.success(depList);
    }

    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable int id){
        log.info("根据id删除部门: {}", id);
        depService.delete(id);
        return Result.success();
    }

    @PostMapping("/depts")
    public Result add(@RequestBody Dep dep){
        log.info("新建部门: {}", dep);
        depService.add(dep);
        return Result.success();
    }

    //更新部门数据时分两步，第一步查询所要修改的部门id，并返回该部门数据
    @GetMapping("/depts/{id}")
    public Result findByid(@PathVariable int id){
        log.info("查询所要修改的部门id: {}", id);
        Dep dep = depService.findByid(id);
        return Result.success(dep);
    }
    //第二步根据id修改部门信息
    @PutMapping("/depts")
    public Result update(@RequestBody Dep dep){
        log.info("根据id修改部门信息");
        depService.update(dep);
        return Result.success();
    }

}
